<?php


namespace app\api\controller;
use think\Db;
use think\Request;

class Enterprise extends Base
{
    public function enterpriseCheck(Request $request)
    {
        if ($request->isPost()){
            $user_id = input('user_id');
            $name = input('name');
            $phone = input('phone');
            $recommend_name = input('recommend_name');

            $business_license = request()->file('business_license');

            $res = [];
            Db::startTrans();
            if (!$user_id || !$name || !$phone || !$business_license){
                $res['status'] = 400;
            }else{

                $ecData['user_id'] = $user_id;
                $ecData['name'] = $name;
                $ecData['phone'] = $phone;
                if ($recommend_name){
                    $ecData['recommend_name'] = $recommend_name;
                }

                $ec_id = db('enterprise_check')->insertGetId($ecData);
                if ($ec_id){
                    if($business_license){
                        $info = $business_license->move(ROOT_PATH . 'public' . DS . 'uploads');
                        if($info){
                            $business_license_path = $_SERVER['SERVER_NAME'].'/uploads/'.$info->getSaveName();

                            $eciData['ec_id'] = $ec_id;
                            $eciData['type'] = 1;
                            $eciData['img'] = $business_license_path;
                            $eci_res = db('enterprise_check_img')->insertGetId($eciData);
                            if ($eci_res){
                                // 缺少多图上传代码
                                $business_product = request()->file('business_product');
                                if ($business_product){
                                    $info_product = $business_product->move(ROOT_PATH . 'public' . DS . 'uploads');
                                    if($info_product){

                                        $business_product_path = $_SERVER['SERVER_NAME'].'/uploads/'.$info->getSaveName();

                                        $_eciData['ec_id'] = $ec_id;
                                        $_eciData['type'] = 2;
                                        $_eciData['img'] = $business_product_path;
                                        $eci_res = db('enterprise_check_img')->insertGetId($_eciData);
                                        if ($eci_res){

                                            $whe['phone'] = $phone;
                                            $up['identity'] = 1
                                            db('user')->where($whe)->update($up)

                                            $res['data']['code'] = 1;
                                            $res['data']['msg'] = '操作成功';
                                            $res['status'] = 200;
                                            Db::commit();
                                        }else{
                                            Db::rollback();
                                            $res['status'] = 400;
                                        }
                                    }else{
                                        Db::rollback();
                                        $res['status'] = 400;
                                        $error = $business_license->getError();
                                        $res['error'] = $error;
                                    }
                                }else{
                                    $res['data']['code'] = 1;
                                    $res['data']['msg'] = '操作成功';
                                    $res['status'] = 200;
                                    Db::commit();
                                }
                            }else{
                                Db::rollback();
                                $res['status'] = 400;
                            }
                        }else{
                            Db::rollback();
                            $res['status'] = 400;
                            $error = $business_license->getError();
                            $res['error'] = $error;
                        }
                    }
                }else{
                    Db::rollback();
                    $res['status'] = 400;
                }

            }
            echo json_encode($res);die;

        }
    }
}